/*******************
Input merchant-level data
Period: March-December 2004
*******************/

clear
clear matrix
clear mata

set more off

cd "Write the path of data folder"

use merchant.dta, clear

format shop %17.0g


label variable shop_transact "Total Transaction Quantity"

label variable shop_value "Total Transaction Value"

label variable shop_fee "Total Merchant Fee"


gen shop_rate = shop_fee/shop_value 
label variable shop_rate "Average Merchant Fee"

gen shop_avgvalue = shop_value/shop_transact
label variable shop_avgvalue "Average Transaction Value"


keep shop shop_transact shop_value shop_fee shop_rate shop_avgvalue

drop if shop_transact == . & shop_value == . &  shop_fee == . &  shop_rate == . &  shop_avgvalue == .  


/*******************
Convert the identifier to string format
Extract code 4-7 for area; 8-11 for industry
*******************/

gen code_s = string(shop,"%17.0g")


gen areacode_s = substr(code_s,-12,4)

gen areacode = real(areacode_s)

drop areacode_s

drop if areacode < 4400 | areacode > 4499

drop if areacode == 4400


gen code4_s = substr(code_s,-8,4)

sort code4_s code_s


/*******************
Define 4 and 87 groups
Assign fraud risk for 87 groups
********************/

do REStat_Define_groups


/*******************
Assign interchange fee and transfer fee
********************/

gen IFee = 0.014 if group5 == 1

replace IFee = 0.007 if group5 == 2

replace IFee = 0.007 if group5 == 3

replace IFee = 0.0035 if group5 == 4

replace IFee = 0 if group5 == 5


gen TFee = 0.002 if group5 == 1

replace TFee = 0.001 if group5 == 2

replace TFee = 0.001 if group5 == 3

replace TFee = 0.0005 if group5 == 4

replace TFee = 0 if group5 == 5


gen ITFee = IFee + TFee

gen shop_AFee = shop_rate - ITFee
label variable shop_AFee "Average Acquirer Fee"


/*******************
Match area data
********************/

sort areacode

merge areacode using area2004.dta

drop if _merge != 3

drop _merge


/*******************
Compute group measures
Imperfect match from MCC to merchant data

Recall: Opincome and Opcost are in Yuan
The other industry data is in 10000 Yuan
********************/

sort group87

merge group87 using industry2005.dta

drop if _merge != 3

drop _merge


/*******************
Count the number of shop in each industry 
Drop the industry with #shop < 4
********************/

gsort group87 -shop_value

by group87: egen group_transact = sum(shop_transact)

gen group_margin = (opincome-opcost)/opincome

gen group_LK = employee/totalasset 


gen one = 1

by group87: egen group_nshop = sum(one)

drop one

drop if group_nshop < 4


/*******************
Define varlables for regression
********************/

gen gdppc_1000 = GDPPC/1000

gen WageEmployed_1000 = WageEmployed/1000

gen napopratio = NAPopulation/Population

gen FIpc10000 = FI/Population 


/*******************
City FE
********************/

gen Chaozhou = 1 if Area=="Chaozhou"
replace Chaozhou = 0 if Chaozhou == .

gen Dongguan = 1 if Area=="Dongguan"
replace Dongguan = 0 if Dongguan == .

gen Foshan = 1 if Area=="Foshan"
replace Foshan = 0 if Foshan == .

gen Guangzhou = 1 if Area=="Guangzhou"
replace Guangzhou = 0 if Guangzhou == .

gen Heyuan = 1 if Area=="Heyuan"
replace Heyuan = 0 if Heyuan == .

gen Huizhou = 1 if Area=="Huizhou"
replace Huizhou = 0 if Huizhou == .

gen Jiangmen = 1 if Area=="Jiangmen"
replace Jiangmen = 0 if Jiangmen == .

gen Jieyang = 1 if Area=="Jieyang"
replace Jieyang = 0 if Jieyang == .

gen Maoming = 1 if Area=="Maoming"
replace Maoming = 0 if Maoming == .

gen Meizhou = 1 if Area=="Meizhou"
replace Meizhou = 0 if Meizhou == .

gen Qingyuan = 1 if Area=="Qingyuan"
replace Qingyuan = 0 if Qingyuan == .

gen Shanwei = 1 if Area=="Shanwei"
replace Shanwei = 0 if Shanwei == .

gen Shantou = 1 if Area=="Shantou"
replace Shantou = 0 if Shantou == .

gen Shaoguan = 1 if Area=="Shaoguan"
replace Shaoguan = 0 if Shaoguan == .

gen Yangjiang = 1 if Area=="Yangjiang"
replace Yangjiang = 0 if Yangjiang == .

gen Yunfu = 1 if Area=="Yunfu"
replace Yunfu = 0 if Yunfu == .

gen Zhanjiang = 1 if Area=="Zhanjiang"
replace Zhanjiang = 0 if Zhanjiang == .

gen Zhaoqing = 1 if Area=="Zhaoqing"
replace Zhaoqing = 0 if Zhaoqing == .

gen Zhongshan = 1 if Area=="Zhongshan"
replace Zhongshan = 0 if Zhongshan == .

gen Zhuhai = 1 if Area=="Zhuhai"
replace Zhuhai = 0 if Zhuhai == .


/*******************
Generate new group index
********************/

sort group87

egen temp = seq() if group87!=group87[_n-1]

by group87: egen group87s = mean(temp)

drop temp


/*******************
Generate IVs
1. The other shops in the same industry-area
2. The other shops in the same industry
********************/

sort group87s areacode

gen shop_transactshare = shop_transact/group_transact


by group87s areacode: egen temp_num = sum(shop_transactshare*shop_avgvalue)

by group87s areacode: egen temp_dem = sum(shop_transactshare)

gen rshop_VPT_T1 = (temp_num-shop_transactshare*shop_avgvalue)/(temp_dem-shop_transactshare)

drop temp_num temp_dem


by group87s: egen temp_num = sum(shop_transactshare*shop_avgvalue)

gen rshop_VPT_T2 = (temp_num-shop_transactshare*shop_avgvalue)/(1-shop_transactshare)

drop temp_num


/*******************
Working sample
Group5=3 loses a lot of data if shop_AFee > 0
  because of the upper limit for Apartment, Vehicle and Motor Home
Also, do not find wholesales industries with upper limit
********************/

drop if shop_AFee <= 0 

drop if group5 == 3

drop if rshop_VPT_T1 == .

* There are 68 observations with AFee <= 0.0000238, i.e. 0.00238%
* For 0.5% fee, AFee is about 0.05%; 
drop if shop_AFee < 0.0009 


/***********************
Variable construction
***********************/

gen Cater = 1 if group5 == 1
replace Cater = 0 if Cater ==.

gen Wel_Pub = 1 if group5 == 4 | group5 == 5
replace Wel_Pub = 0 if Wel_Pub ==.


gen VPT = shop_avgvalue

gen VPT_AFee = shop_avgvalue*shop_AFee

gen VPT_ITFee = shop_avgvalue*ITFee

gen VPT_Margin = VPT*group_margin

gen VPT_LK = VPT*group_LK


gen VPT_Cater = shop_avgvalue*Cater

gen VPT_Wel_Pub = shop_avgvalue*Wel_Pub


gen VPT_Chaozhou = VPT*Chaozhou 
gen VPT_Dongguan = VPT*Dongguan
gen VPT_Foshan = VPT*Foshan 
gen VPT_Guangzhou = VPT*Guangzhou 
gen VPT_Heyuan = VPT*Heyuan 
gen VPT_Huizhou = VPT*Huizhou 
gen VPT_Jiangmen = VPT*Jiangmen 
gen VPT_Jieyang = VPT*Jieyang 
gen VPT_Maoming = VPT*Maoming 
gen VPT_Meizhou = VPT*Meizhou
gen VPT_Qingyuan = VPT*Qingyuan 
gen VPT_Shantou = VPT*Shantou
gen VPT_Shanwei = VPT*Shanwei
gen VPT_Shaoguan = VPT*Shaoguan
gen VPT_Yangjiang = VPT*Yangjiang
gen VPT_Yunfu = VPT*Yunfu
gen VPT_Zhanjiang = VPT*Zhanjiang
gen VPT_Zhaoqing = VPT*Zhaoqing
gen VPT_Zhongshan = VPT*Zhongshan
gen VPT_Zhuhai = VPT*Zhuhai


/***********************
IV construction
***********************/

gen rVPT_ITFee = rshop_VPT_T1*ITFee

gen rVPT_Margin = rshop_VPT_T1*group_margin

gen rVPT_LK = rshop_VPT_T1*group_LK


gen rVPT_Cater = rshop_VPT_T1*Cater

gen rVPT_Wel_Pub = rshop_VPT_T1*Wel_Pub


gen rVPT_Chaozhou = rshop_VPT_T1*Chaozhou 
gen rVPT_Dongguan = rshop_VPT_T1*Dongguan
gen rVPT_Foshan = rshop_VPT_T1*Foshan 
gen rVPT_Guangzhou = rshop_VPT_T1*Guangzhou 
gen rVPT_Heyuan = rshop_VPT_T1*Heyuan 
gen rVPT_Huizhou = rshop_VPT_T1*Huizhou 
gen rVPT_Jiangmen = rshop_VPT_T1*Jiangmen 
gen rVPT_Jieyang = rshop_VPT_T1*Jieyang 
gen rVPT_Maoming = rshop_VPT_T1*Maoming 
gen rVPT_Meizhou = rshop_VPT_T1*Meizhou
gen rVPT_Qingyuan = rshop_VPT_T1*Qingyuan 
gen rVPT_Shantou = rshop_VPT_T1*Shantou
gen rVPT_Shanwei = rshop_VPT_T1*Shanwei
gen rVPT_Shaoguan = rshop_VPT_T1*Shaoguan
gen rVPT_Yangjiang = rshop_VPT_T1*Yangjiang
gen rVPT_Yunfu = rshop_VPT_T1*Yunfu
gen rVPT_Zhanjiang = rshop_VPT_T1*Zhanjiang
gen rVPT_Zhaoqing = rshop_VPT_T1*Zhaoqing
gen rVPT_Zhongshan = rshop_VPT_T1*Zhongshan
gen rVPT_Zhuhai = rshop_VPT_T1*Zhuhai


/***********************
Robustness Checks: Alternative IV 
To get Column 6 of Table 5
Run the following program segment, and
comment the previous IV construction 
***********************/
/*
gen rVPT_ITFee = rshop_VPT_T2*ITFee

gen rVPT_Margin = rshop_VPT_T2*group_margin

gen rVPT_LK = rshop_VPT_T2*group_LK


gen rVPT_Cater = rshop_VPT_T2*Cater

gen rVPT_Wel_Pub = rshop_VPT_T2*Wel_Pub


gen rVPT_Chaozhou = rshop_VPT_T2*Chaozhou 
gen rVPT_Dongguan = rshop_VPT_T2*Dongguan
gen rVPT_Foshan = rshop_VPT_T2*Foshan 
gen rVPT_Guangzhou = rshop_VPT_T2*Guangzhou 
gen rVPT_Heyuan = rshop_VPT_T2*Heyuan 
gen rVPT_Huizhou = rshop_VPT_T2*Huizhou 
gen rVPT_Jiangmen = rshop_VPT_T2*Jiangmen 
gen rVPT_Jieyang = rshop_VPT_T2*Jieyang 
gen rVPT_Maoming = rshop_VPT_T2*Maoming 
gen rVPT_Meizhou = rshop_VPT_T2*Meizhou
gen rVPT_Qingyuan = rshop_VPT_T2*Qingyuan 
gen rVPT_Shantou = rshop_VPT_T2*Shantou
gen rVPT_Shanwei = rshop_VPT_T2*Shanwei
gen rVPT_Shaoguan = rshop_VPT_T2*Shaoguan
gen rVPT_Yangjiang = rshop_VPT_T2*Yangjiang
gen rVPT_Yunfu = rshop_VPT_T2*Yunfu
gen rVPT_Zhanjiang = rshop_VPT_T2*Zhanjiang
gen rVPT_Zhaoqing = rshop_VPT_T2*Zhaoqing
gen rVPT_Zhongshan = rshop_VPT_T2*Zhongshan
gen rVPT_Zhuhai = rshop_VPT_T2*Zhuhai


* Column 6

gmm ( VPT_AFee*(1+exp({b_LK}*group_LK+{b1}*Cater+{b45}*Wel_Pub)) - {b_Cost}*exp({b_LK}*group_LK+{b1}*Cater+{b45}*Wel_Pub) + VPT_ITFee ///
- {AM_Margin}*VPT_Margin - {AM_LK}*VPT_LK  - {AM1}*VPT_Cater - {AM45}*VPT_Wel_Pub - ({AMr:VPT_Chaozhou VPT_Dongguan VPT_Foshan VPT_Guangzhou VPT_Heyuan VPT_Huizhou VPT_Jiangmen VPT_Jieyang VPT_Maoming VPT_Meizhou ///
VPT_Qingyuan VPT_Shantou VPT_Shanwei VPT_Shaoguan VPT_Yangjiang VPT_Yunfu VPT_Zhanjiang VPT_Zhaoqing VPT_Zhongshan VPT_Zhuhai}) ), ///
instruments(FIpc10000 rVPT_Margin rVPT_LK rVPT_Cater rVPT_Wel_Pub group_LK Cater Wel_Pub ///
rVPT_Chaozhou rVPT_Dongguan rVPT_Foshan rVPT_Guangzhou rVPT_Heyuan rVPT_Huizhou rVPT_Jiangmen rVPT_Jieyang rVPT_Maoming rVPT_Meizhou ///
rVPT_Qingyuan rVPT_Shantou rVPT_Shanwei rVPT_Shaoguan rVPT_Yangjiang rVPT_Yunfu rVPT_Zhanjiang rVPT_Zhaoqing rVPT_Zhongshan rVPT_Zhuhai) conv_maxiter(400) from(b_LK 5 b1 3 b45 -2 b_Cost 3)

estat overid
*/

/***********************
Robustness Checks: Contract Length
***********************/

* Column 3-5

gmm ( VPT_AFee*(1+exp({b_LK}*group_LK+{b1}*Cater+{b45}*Wel_Pub)) - {b_Cost}*exp({b_LK}*group_LK+{b1}*Cater+{b45}*Wel_Pub) + VPT_ITFee ///
- 1.1*{AM_Margin}*VPT_Margin - 1.1*{AM_LK}*VPT_LK  - 1.1*{AM1}*VPT_Cater - 1.1*{AM45}*VPT_Wel_Pub - 1.1*({AMr:VPT_Chaozhou VPT_Dongguan VPT_Foshan VPT_Guangzhou VPT_Heyuan VPT_Huizhou VPT_Jiangmen VPT_Jieyang VPT_Maoming VPT_Meizhou ///
VPT_Qingyuan VPT_Shantou VPT_Shanwei VPT_Shaoguan VPT_Yangjiang VPT_Yunfu VPT_Zhanjiang VPT_Zhaoqing VPT_Zhongshan VPT_Zhuhai}) ), ///
instruments(FIpc10000 rVPT_Margin rVPT_LK rVPT_Cater rVPT_Wel_Pub group_LK Cater Wel_Pub ///
rVPT_Chaozhou rVPT_Dongguan rVPT_Foshan rVPT_Guangzhou rVPT_Heyuan rVPT_Huizhou rVPT_Jiangmen rVPT_Jieyang rVPT_Maoming rVPT_Meizhou ///
rVPT_Qingyuan rVPT_Shantou rVPT_Shanwei rVPT_Shaoguan rVPT_Yangjiang rVPT_Yunfu rVPT_Zhanjiang rVPT_Zhaoqing rVPT_Zhongshan rVPT_Zhuhai) conv_maxiter(400) from(b_LK 5 b1 3 b45 -2 b_Cost 3)

estat overid

gmm ( VPT_AFee*(1+exp({b_LK}*group_LK+{b1}*Cater+{b45}*Wel_Pub)) - {b_Cost}*exp({b_LK}*group_LK+{b1}*Cater+{b45}*Wel_Pub) + VPT_ITFee ///
- 1.5*{AM_Margin}*VPT_Margin - 1.5*{AM_LK}*VPT_LK  - 1.5*{AM1}*VPT_Cater - 1.5*{AM45}*VPT_Wel_Pub - 1.5*({AMr:VPT_Chaozhou VPT_Dongguan VPT_Foshan VPT_Guangzhou VPT_Heyuan VPT_Huizhou VPT_Jiangmen VPT_Jieyang VPT_Maoming VPT_Meizhou ///
VPT_Qingyuan VPT_Shantou VPT_Shanwei VPT_Shaoguan VPT_Yangjiang VPT_Yunfu VPT_Zhanjiang VPT_Zhaoqing VPT_Zhongshan VPT_Zhuhai}) ), ///
instruments(FIpc10000 rVPT_Margin rVPT_LK rVPT_Cater rVPT_Wel_Pub group_LK Cater Wel_Pub ///
rVPT_Chaozhou rVPT_Dongguan rVPT_Foshan rVPT_Guangzhou rVPT_Heyuan rVPT_Huizhou rVPT_Jiangmen rVPT_Jieyang rVPT_Maoming rVPT_Meizhou ///
rVPT_Qingyuan rVPT_Shantou rVPT_Shanwei rVPT_Shaoguan rVPT_Yangjiang rVPT_Yunfu rVPT_Zhanjiang rVPT_Zhaoqing rVPT_Zhongshan rVPT_Zhuhai) conv_maxiter(400) from(b_LK 5 b1 3 b45 -2 b_Cost 3)

estat overid

gmm ( VPT_AFee*(1+exp({b_LK}*group_LK+{b1}*Cater+{b45}*Wel_Pub)) - {b_Cost}*exp({b_LK}*group_LK+{b1}*Cater+{b45}*Wel_Pub) + VPT_ITFee ///
- 2*{AM_Margin}*VPT_Margin - 2*{AM_LK}*VPT_LK  - 2*{AM1}*VPT_Cater - 2*{AM45}*VPT_Wel_Pub - 2*({AMr:VPT_Chaozhou VPT_Dongguan VPT_Foshan VPT_Guangzhou VPT_Heyuan VPT_Huizhou VPT_Jiangmen VPT_Jieyang VPT_Maoming VPT_Meizhou ///
VPT_Qingyuan VPT_Shantou VPT_Shanwei VPT_Shaoguan VPT_Yangjiang VPT_Yunfu VPT_Zhanjiang VPT_Zhaoqing VPT_Zhongshan VPT_Zhuhai}) ), ///
instruments(FIpc10000 rVPT_Margin rVPT_LK rVPT_Cater rVPT_Wel_Pub group_LK Cater Wel_Pub ///
rVPT_Chaozhou rVPT_Dongguan rVPT_Foshan rVPT_Guangzhou rVPT_Heyuan rVPT_Huizhou rVPT_Jiangmen rVPT_Jieyang rVPT_Maoming rVPT_Meizhou ///
rVPT_Qingyuan rVPT_Shantou rVPT_Shanwei rVPT_Shaoguan rVPT_Yangjiang rVPT_Yunfu rVPT_Zhanjiang rVPT_Zhaoqing rVPT_Zhongshan rVPT_Zhuhai) conv_maxiter(400) from(b_LK 5 b1 3 b45 -2 b_Cost 3)

estat overid


/***********************
Robustness Checks: Cost Bound at 50% AFee per transaction = 1.622
***********************/

* Column 7

gmm ( VPT_AFee*(1+exp({b_LK}*group_LK+{b1}*Cater+{b45}*Wel_Pub)) - (1.622)*exp({b_LK}*group_LK+{b1}*Cater+{b45}*Wel_Pub) + VPT_ITFee ///
- {AM_Margin}*VPT_Margin - {AM_LK}*VPT_LK  - {AM1}*VPT_Cater - {AM45}*VPT_Wel_Pub - ({AMr:VPT_Chaozhou VPT_Dongguan VPT_Foshan VPT_Guangzhou VPT_Heyuan VPT_Huizhou VPT_Jiangmen VPT_Jieyang VPT_Maoming VPT_Meizhou ///
VPT_Qingyuan VPT_Shantou VPT_Shanwei VPT_Shaoguan VPT_Yangjiang VPT_Yunfu VPT_Zhanjiang VPT_Zhaoqing VPT_Zhongshan VPT_Zhuhai}) ), ///
instruments(FIpc10000 rVPT_Margin rVPT_LK rVPT_Cater rVPT_Wel_Pub group_LK Cater Wel_Pub ///
rVPT_Chaozhou rVPT_Dongguan rVPT_Foshan rVPT_Guangzhou rVPT_Heyuan rVPT_Huizhou rVPT_Jiangmen rVPT_Jieyang rVPT_Maoming rVPT_Meizhou ///
rVPT_Qingyuan rVPT_Shantou rVPT_Shanwei rVPT_Shaoguan rVPT_Yangjiang rVPT_Yunfu rVPT_Zhanjiang rVPT_Zhaoqing rVPT_Zhongshan rVPT_Zhuhai) conv_maxiter(400) from(b_LK 5 b1 3 b45 -2)

estat overid


/***********************
Robustness Checks: Skip Group 1 + Additional Z
***********************/

* Construct additional variable in Z

gen Margin_LK = group_margin*group_LK

* Add Margin_LK into Z
* Column 8

gmm ( VPT_AFee*(1+exp({b_LK}*group_LK+{b45}*Wel_Pub)) - {b_Cost}*exp({b_LK}*group_LK+{b45}*Wel_Pub) + VPT_ITFee ///
- {AM_Margin}*VPT_Margin - {AM_LK}*VPT_LK - {AM45}*VPT_Wel_Pub - ({AMr:VPT_Chaozhou VPT_Dongguan VPT_Foshan VPT_Guangzhou VPT_Heyuan VPT_Huizhou VPT_Jiangmen VPT_Jieyang VPT_Maoming VPT_Meizhou ///
VPT_Qingyuan VPT_Shantou VPT_Shanwei VPT_Shaoguan VPT_Yangjiang VPT_Yunfu VPT_Zhanjiang VPT_Zhaoqing VPT_Zhongshan VPT_Zhuhai}) ) if Cater==0, ///
instruments(FIpc10000 Margin_LK rVPT_Margin rVPT_LK rVPT_Wel_Pub group_LK Wel_Pub ///
rVPT_Chaozhou rVPT_Dongguan rVPT_Foshan rVPT_Guangzhou rVPT_Heyuan rVPT_Huizhou rVPT_Jiangmen rVPT_Jieyang rVPT_Maoming rVPT_Meizhou ///
rVPT_Qingyuan rVPT_Shantou rVPT_Shanwei rVPT_Shaoguan rVPT_Yangjiang rVPT_Yunfu rVPT_Zhanjiang rVPT_Zhaoqing rVPT_Zhongshan rVPT_Zhuhai) conv_maxiter(400) from(b_LK 5 b45 -2 b_Cost 2)

estat overid
